package com.spzx.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.cart.api.RemoteCartService;
import com.spzx.cart.api.domain.CartInfo;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.utils.bean.BeanUtils;
import com.spzx.order.domain.OrderInfo;
import com.spzx.order.domain.OrderItem;
import com.spzx.order.mapper.OrderInfoMapper;
import com.spzx.order.service.OrderInfoService;
import com.spzx.order.vo.TradeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-05-09
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {

    @Autowired
    private RemoteCartService cartService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public TradeVo trade() {
        //1.创建vo对象
        TradeVo tradeVo = new TradeVo();
        //2.远程调用服务,获取选择商品列表,封装到订单明细中
        R<List<CartInfo>> r = cartService.getCartCheckedList(SecurityConstants.INNER);
        //2.1验证是否调用成功
        if(R.FAIL==r.getCode()){
            throw new ServiceException("远程购物车调用失败原因:"+r.getMsg());
        }
        //2.2获取调用的商品列表
        List<CartInfo> cartInfoList = r.getData();
        //2.3判断购物车商品列表表是否为空
        if(CollectionUtils.isEmpty(cartInfoList)){
            throw  new ServiceException("没有需要结算的商品");
        }
        //2.4把购物车商品列表转为订单明细表
        List<OrderItem> orderItemList = cartInfoList.stream().
                map(cartInfo -> {
                    OrderItem orderItem = new OrderItem();
                    BeanUtils.copyProperties(cartInfo, orderItem);
                    return orderItem;
                }).collect(Collectors.toList());
        tradeVo.setOrderItemList(orderItemList);
        System.out.println(orderItemList+"************");
        //3.计算商品金额
        BigDecimal bigDecimal = new BigDecimal(0);
        for (OrderItem orderItem : orderItemList) {
            bigDecimal=bigDecimal.add(orderItem.getSkuPrice().
                        multiply(new BigDecimal(orderItem.getSkuNum())));
        }
        tradeVo.setTotalAmount(bigDecimal);
        //4.生成流水号-防止重复提交订单 存入redis 中 有效时间为五分钟
         String tradeNo   =this.generateTradeNo();

        //5.封装为结算对象vo响应
        tradeVo.setTradeNo(tradeNo);
        return tradeVo;
    }
    /**
     * 生成当前用户结算订单流水号
     *
     * @return
     */
    @Override
    public String generateTradeNo() {
        String tradeKey= "user:tradeNo:"+ SecurityContextHolder.getUserId();
        String tradeNo= UUID.randomUUID().toString();
        //redis 用于操作 string 类型的数据
        redisTemplate.opsForValue().set(tradeKey,tradeNo,5, TimeUnit.SECONDS);
        return tradeNo;
    }
}
